﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Drawing;
using System.Windows.Forms;

namespace RoketPack
{
    class IconAnimator
    {
        private System.Timers.Timer m_Timer;
        private NotifyIcon m_NotifyIcon;
        private List<Icon> m_Icons;
        private Icon m_DefaultIcon;
        private static bool m_AeroChecked = false;
        private static bool m_AeroEnabled = false;
        private static readonly List<Icon> p_DarkUpdateSet = (new Icon[] {
                                               Properties.Resources.DarkUpdateCheck1,
                                               Properties.Resources.DarkUpdateCheck2,
                                               Properties.Resources.DarkUpdateCheck3,
                                               Properties.Resources.DarkUpdateCheck4,
                                               Properties.Resources.DarkUpdateCheck5,
                                               Properties.Resources.DarkUpdateCheck6,
                                               Properties.Resources.DarkUpdateCheck7,
                                               Properties.Resources.DarkUpdateCheck8,
                                               Properties.Resources.DarkUpdateCheck9,
                                               Properties.Resources.DarkUpdateCheck10
                                           }).ToList<Icon>();
        private static readonly List<Icon> p_DarkInstallSet = (new Icon[] {
                                               Properties.Resources.DarkInstall1,
                                               Properties.Resources.DarkInstall2,
                                               Properties.Resources.DarkInstall3,
                                               Properties.Resources.DarkInstall4,
                                               Properties.Resources.DarkInstall5,
                                               Properties.Resources.DarkInstall6,
                                               Properties.Resources.DarkInstall7,
                                               Properties.Resources.DarkInstall8,
                                               Properties.Resources.DarkInstall9,
                                               Properties.Resources.DarkInstall10
                                           }).ToList<Icon>();
        private static readonly List<Icon> p_LightUpdateSet = (new Icon[] {
                                               Properties.Resources.LightUpdateCheck1,
                                               Properties.Resources.LightUpdateCheck2,
                                               Properties.Resources.LightUpdateCheck3,
                                               Properties.Resources.LightUpdateCheck4,
                                               Properties.Resources.LightUpdateCheck5,
                                               Properties.Resources.LightUpdateCheck6,
                                               Properties.Resources.LightUpdateCheck7,
                                               Properties.Resources.LightUpdateCheck8,
                                               Properties.Resources.LightUpdateCheck9,
                                               Properties.Resources.LightUpdateCheck10
                                           }).ToList<Icon>();
        private static readonly List<Icon> p_LightInstallSet = (new Icon[] {
                                               Properties.Resources.LightInstall1,
                                               Properties.Resources.LightInstall2,
                                               Properties.Resources.LightInstall3,
                                               Properties.Resources.LightInstall4,
                                               Properties.Resources.LightInstall5,
                                               Properties.Resources.LightInstall6,
                                               Properties.Resources.LightInstall7,
                                               Properties.Resources.LightInstall8,
                                               Properties.Resources.LightInstall9,
                                               Properties.Resources.LightInstall10
                                           }).ToList<Icon>();

        /// <summary>
        /// A set of icons used when repositories or other meta-information is
        /// being updated.
        /// </summary>
        internal static List<Icon> UpdateSet
        {
            get
            {
                if (!IconAnimator.m_AeroChecked)
                    IconAnimator.m_AeroEnabled = Aero.Enabled;

                if (IconAnimator.m_AeroEnabled)
                    return IconAnimator.p_LightUpdateSet;
                else
                    return IconAnimator.p_DarkUpdateSet;
            }
        }

        /// <summary>
        /// A set of icons used when applications or application updates are being
        /// downloaded or installed.
        /// </summary>
        internal static List<Icon> InstallSet
        {
            get
            {
                if (!IconAnimator.m_AeroChecked)
                    IconAnimator.m_AeroEnabled = Aero.Enabled;

                if (IconAnimator.m_AeroEnabled)
                    return IconAnimator.p_LightInstallSet;
                else
                    return IconAnimator.p_DarkInstallSet;
            }
        }

        /// <summary>
        /// Sets up an IconAnimator, which uses threads to rotate the system tray icon while an operation
        /// is in progress.
        /// </summary>
        /// <param name="tray">The system tray icon to modify.</param>
        /// <param name="icons">The icons to rotate through.</param>
        /// <param name="defaulticon">The icon to set the system tray back to after Stop() is called.</param>
        /// <param name="interval">The interval between switching images.</param>
        internal IconAnimator(NotifyIcon tray, List<Icon> icons, Icon defaulticon, int interval)
        {
            this.m_NotifyIcon = tray;
            this.m_Icons = icons;
            this.m_DefaultIcon = defaulticon;
            this.m_Timer = new System.Timers.Timer(interval);
            this.m_Timer.Elapsed += new ElapsedEventHandler(OnTick);
            this.m_Timer.Start();
        }

        /// <summary>
        /// Stops the IconAnimator.
        /// </summary>
        internal void Stop()
        {
            this.m_Timer.Stop();
            this.m_NotifyIcon.Icon = this.m_DefaultIcon;
        }

        private void OnTick(object sender, ElapsedEventArgs e)
        {
            int i = this.m_Icons.IndexOf(this.m_NotifyIcon.Icon);
            if (i == this.m_Icons.Count - 1)
                i = 0;
            else
                i += 1;
            this.m_NotifyIcon.Icon = this.m_Icons[i];
        }
    }
}
